/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.business;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author david
 */
public class DateRange implements Cloneable {

    private static GregorianCalendar CALENDAR = new GregorianCalendar();

    private Date fromDate;
    private Date toDate;

    public DateRange() {
    }

    public DateRange(Date fromDate, Date toDate) {
        this.fromDate = fromDate;
        this.toDate = toDate;
    }

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }

    public boolean contains(Date date) {
        return (fromDate == null || !date.before(fromDate)) &&
                (toDate == null || !date.after(toDate));
    }

    public boolean contains(DateRange dateRange) {
        return contains(dateRange.fromDate) &&
                contains(dateRange.toDate);
    }

    public int getNumberOfDays() {

        GregorianCalendar startCal = new GregorianCalendar();
        startCal.setTime(fromDate);

        GregorianCalendar endCal = new GregorianCalendar();
        endCal.setTime(toDate);

        long millisenconds = toDate.getTime() - fromDate.getTime();
        int days = 1 + Math.round(millisenconds / (24 * 60 * 60 * 1000));
        
        return days;
    }

    public void reset() {
        fromDate = DaisyChainSystem.getToday().getTime();
        toDate = DaisyChainSystem.getToday().getTime();
    }

    public void addRange(Date fromDate, Date toDate) {
        if (fromDate != null && fromDate.before(this.fromDate)) {
            this.fromDate = fromDate;
        }
        if (toDate != null && toDate.after(this.toDate)) {
            this.toDate = toDate;
        }
    }

    public void addRange(GregorianCalendar fromCal, GregorianCalendar toCal) {
        if (fromCal != null && fromCal.getTime().before(this.fromDate)) {
            this.fromDate = fromCal.getTime();
        }
        if (toDate != null && toCal.getTime().after(this.toDate)) {
            this.toDate = toCal.getTime();
        }
    }

    public void expandLower(int field, int noOfDays) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(fromDate);
        cal.add(field, -noOfDays);
        fromDate = cal.getTime();
    }

    public void expandUpper(int field, int noOfDays) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(toDate);
        cal.add(field, noOfDays);
        toDate = cal.getTime();
    }

    public void expand(int field, int noOfField) {
        expandLower(field, noOfField);
        expandUpper(field, noOfField);
    }

    @Override
    public String toString() {
        CALENDAR.setTime(fromDate);
        return DaisyChainUtilities.dateAsString(fromDate) + " - " +
                DaisyChainUtilities.dateAsString(toDate);
    }

    @Override
    public DateRange clone() {
        return this.clone();
    }
}
